﻿using System;
using System.Data;
using ServerApi.Controllers;
using TuyennvLibrary.Notify;
using TuyennvLibrary.Notify.Winphone;
using TuyennvLibrary.ServerApi.Models;
using TuyennvLibrary.ServerApi.ClassApi;

namespace ServerApi.administrator
{
    public partial class SendMessageAllDevices : System.Web.UI.Page
    {
        ApplicationModel appModel = new ApplicationModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            AdminController.checkLogin(Session, Request, Response);
            AdminController.checkPermission(Page, this.GetType(), Session, Request, Response);
            if (!IsPostBack)
            {
                setDanhsachUngDung();
            }
        }

        protected void ddlPlatform_SelectedIndexChanged(object sender, EventArgs e)
        {
            setDanhsachUngDung();
        }
        private void setDanhsachUngDung()
        {
            DataTable dt = appModel.GetListBy(new string[] { "status_advertise", "status", "platform" }, new object[] { 1, 1, int.Parse(ddlPlatform.Text) }, "name");
            ddlApplication.DataSource = dt;
            ddlApplication.DataTextField = "name";
            ddlApplication.DataValueField = "appid";
            ddlApplication.DataBind();
        }
        protected void btnSend_Click(object sender, EventArgs e)
        {
            BaseApi api = new GeneralApi(ddlApplication.Text);
            if (int.Parse(ddlPlatform.Text) == 3)
            {
                WinphoneNotify notify = new WinphoneNotify();
                notify.Title = txtTitle.Text;
                notify.Message = txtContent.Text;
                notify.PageXaml = "MainPage.xaml";
                notify.AddParam("Type", int.Parse(ddlType.Text));
                notify.AddParam("TypeNotify", 0);
                notify.AddParam("Url", txtUrl.Text);
                NotifyResponse response= api.notifyWinphone(notify);
                txtResponse.Text = response.ToString();
            }
            else
            {
                var data = new { TypeNotify = 0, Type = int.Parse(ddlType.Text), Title = txtTitle.Text, Content = txtContent.Text, Url = txtUrl.Text };
               
                NotifyResponse response = api.nofityContent(int.Parse(ddlPlatform.Text), txtTitle.Text, data);
                txtResponse.Text = response.ToString();
            }
            
        }
    }
}